package com.rentcars.controller;

import com.github.pagehelper.PageInfo;
import com.rentcars.entity.Account;
import com.rentcars.entity.Dept;
import com.rentcars.entity.Role;
import com.rentcars.mapper.DeptMapper;
import com.rentcars.mapper.RoleMapper;
import com.rentcars.service.AccountService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * 类描述：账户管理
 *
 * @author 张吉哲
 * @date 2021/3/1 14:06
 */
@RestController
@RequestMapping("/accounts")
public class AccountController {

  private final AccountService accountService;
  private final RoleMapper roleMapper;
  private final DeptMapper deptMapper;

  public AccountController(AccountService accountService, RoleMapper roleMapper,
      DeptMapper deptMapper) {
    this.accountService = accountService;
    this.roleMapper = roleMapper;
    this.deptMapper = deptMapper;
  }

  /**
   * 进入账户管理界面
   *
   * @return 账户管理界面地址
   */
  @RequestMapping("")
  public ModelAndView accountList() {
    return new ModelAndView("account_list");
  }

  /**
   * 显示所有账户信息
   *
   * @param account  搜素条件
   * @param pageSize 每页显示条数
   * @param curr     当前页
   * @return 显示结果
   */
  @RequestMapping(value = "/selectAll", method = RequestMethod.GET)
  public PageInfo<Account> accounts(Account account, Integer pageSize, Integer curr) {
    return accountService.selectAllAccount(account, curr, pageSize);
  }

  /**
   * 跳转到添加账户界面
   *
   * @return 添加页面地址
   */
  @RequestMapping(value = "/accountAdd")
  public ModelAndView accountAdd() {
    return new ModelAndView("account_add");
  }

  /**
   * 查询所有租赁门店和角色信息
   *
   * @return 门店和角色集合
   */
  @RequestMapping(value = "/selectRolesAndDepts", method = RequestMethod.GET)
  public Map<String, Object> selectRolesAndDepts() {
    Map<String, Object> map = new HashMap<>(16);
    List<Role> roles = roleMapper.selectAllRoles();
    List<Dept> depts = deptMapper.selectAllDeptIdAndName();
    map.put("roles", roles);
    map.put("depts", depts);
    return map;
  }

  /**
   * 添加账户
   *
   * @param account 添加的账户信息
   * @param session 用来获取当前登录的管理员信息
   * @return 是否添加成功
   */
  @RequestMapping(value = "/add", method = RequestMethod.POST)
  public Boolean add(Account account, HttpSession session) {
    account.setAdduser("1");
    return accountService.insertAccount(account);
  }

  /**
   * 批量删除
   *
   * @param checkUid 被删除的账户集合
   * @return 是否成功
   */
  @RequestMapping(value = "/delete", method = RequestMethod.POST)
  public Boolean delete(String[] checkUid) {
    return accountService.deleteAccountsByUids(checkUid);
  }

  /**
   * 跳转到修改账户界面
   *
   * @param uid 被修改用户的id
   * @return 修改账户界面，账户信息
   */
  @RequestMapping(value = "/editPage/{uid}", method = RequestMethod.GET)
  public ModelAndView editAccount(@PathVariable String uid) {
    Account account = accountService.selectAccountByPrimaryKey(uid);
    List<Account> accounts = new ArrayList<>();
    accounts.add(account);
    List<Role> roles = roleMapper.selectAllRoles();
    List<Dept> depts = deptMapper.selectAllDeptIdAndName();
    Map<String, List> map = new HashMap<>(16);
    map.put("accounts", accounts);
    map.put("roles", roles);
    map.put("depts", depts);
    return new ModelAndView("account_edit", map);
  }

  /**
   * 修改账户信息
   *
   * @param account 修改后的账户信息
   * @return 是否成功
   */
  @RequestMapping(value = "/editAccount", method = RequestMethod.POST)
  public Boolean editAccount(Account account) {
    account.setEdituser("1");
    return accountService.updateAccountByPrimaryKey(account);
  }

  /**
   * 判断用户名是否重复
   *
   * @param username 被检测用户名
   * @return 重复返回false，不重复返回true
   */
  @RequestMapping(value = "/isUsername", method = RequestMethod.POST)
  @Validated
  public Boolean isUsername(String username, String uid) {
    return accountService.selectAccountByUsername(username, uid);
  }

  /**
   * 删除一个账号
   *
   * @param uid 账号id
   * @return 是否成功
   */
  @RequestMapping(value = "/delOneAccount/{uid}", method = RequestMethod.GET)
  public Boolean delOneAccount(@PathVariable String uid) {
    return accountService.deleteAccountsByUid(uid);
  }

  /**
   * 修改该账户禁用/启用
   *
   * @param uid 修改账户的id
   * @return 是否成功
   */
  @RequestMapping(value = "/adminStateChange/{uid}", method = RequestMethod.GET)
  public Boolean adminStateChange(@PathVariable String uid, Integer userstate) {
    return accountService.updateAccountStateByUid(uid, userstate);
  }
}
